package com.ydgk.servlet;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Book;
import com.ydgk.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "FindAllBookServlet", urlPatterns = "/FindAllBookServlet.action")
public class FindAllBookServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");

        BookDao bookDao = new BookDao();

        String pn = request.getParameter("pageNumber");
        String pc = request.getParameter("pageCount");
        int pageNumber = 1;
        int pageCount = 10 ;
        try {

            pageNumber = Integer.parseInt(pn);
            pageCount = Integer.parseInt(pc);
        }catch (NumberFormatException e){
            pageNumber = 1;
            pageCount = 10 ;
        }

        bookDao.pageCount = pageCount;
        List<Book> bookList = bookDao.paging(pageNumber);
        int total = bookDao.total();


        HashMap map = new HashMap() ;

        map.put("code",0);
        map.put("msg","");
        //map.put("count",123456);
        map.put("data",bookList);
        map.put("pageCount",pageCount);
        map.put("pageNumber",pageNumber);
        map.put("total",total);
        String str = JSON.toJSONString(map);
        response.getWriter().print(str);
    }
}
